import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

/**
 * @program: leetcode085
 * @author: JCM
 * @create: 2023-02-27 20:23
 * @description:
 **/
public class Main {

    public int maximalRectangle(char[][] matrix) {
        int row = matrix.length;
        int col = matrix[0].length;
        if(row == 0) {
            return 0;
        }
        int[][] width = new int[row][col];
        int ans = 0;
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if(matrix[i][j] == '1') {
                    width[i][j] = j > 0 ? width[i][j-1]+1 : 1;
                }else {
                    width[i][j] = 0;
                }
                // 记录所有行中最小的数
                int minWidth = width[i][j];
                // 向上扩展行
                for(int up_row = i; up_row >= 0; up_row--) {
                    int height = i - up_row + 1;
                    // 寻找最小的数作为矩阵的宽
                    minWidth = Math.min(minWidth,width[up_row][j]);
                    // 更新面积
                    ans = Math.max(ans,height*minWidth);
                }
            }

        }
        return ans;

    }

}